$(function(){
    var addr = $("#addr");
    var top = getTop(addr.get(0))-22;	//jquery对象转换为dom对象
    var left = getLeft(addr.get(0))-38;
    var width = getWith(addr.get(0));
    var height = getHeight(addr.get(0));
    //alert(top+","+left+","+width+","+height);
    var table = $(".table");

    //添加按键事件
    addr.bind("input propertychange",function(){
        var value = addr.val();
        if(!value){
            table.empty();
            return;
        }
        sendGet("addrSearch?cityName="+value,parseData);
    });
    
    //发送请求
    function sendGet(url,fn){
        $.ajax({
            type:"GET",
            url:url,
            dataType:"json"
        }).done(function(msg){
            fn(msg);
        })
    }
    
    //解析数据
    function parseData(data){
        console.log(data.length);
        table.empty();
        table.css({});
        if(!data.length){
            return;
        }
        table.css({"margin-left":left,"margin-top":top,"border":"1px solid blue","width":width});
        data.forEach((item,index)=>{
            var tr = $("<tr></tr");
            var td = $("<td></td");
            td.text(item.area);
            tr.append(td);
            table.append(tr);
        });
    }
});



//获取控件的偏移位置
function getTop(e) {  
    var t=e.offsetTop;   
    while(e=e.offsetParent) {  
         t+=e.offsetTop;   
     } 
     return t; 
}
function getLeft(e){
	var l = e.offsetLeft;
	while(e=e.offsetParent){
		l+=e.offsetLeft;
	}
	return l;
}
function getWith(e){
	var w = e.offsetWidth;
	return w;
}
function getHeight(e){
	var h = e.offsetHeight;
	return h;
}